/*
 * Copyright (c) Thorben Linneweber and others
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Jitter2.Dynamics;
using Jitter2.Dynamics.Constraints;
using Jitter2.LinearMath;
using Jitter2.UnmanagedMemory;

using SoftFloat;

namespace Jitter2.SoftBodies
{
	
	/// <summary>
	/// Constrains the distance between a fixed point in the reference frame of one body and a fixed
	/// point in the reference frame of another body. This constraint removes one translational degree
	/// of freedom. For a distance of zero, use the <see cref="BallSocket"/> constraint.
	/// </summary>
	public unsafe class SpringConstraint : Constraint
	{
	    [StructLayout(LayoutKind.Sequential)]
	    public struct SpringData
	    {
	        internal int _internal;
	        public delegate*<ref ConstraintData, void> Iterate;
	        public delegate*<ref ConstraintData, sfloat, void> PrepareForIteration;
	
	        public JHandle<RigidBodyData> Body1;
	        public JHandle<RigidBodyData> Body2;
	
	        public NVector3 LocalAnchor1;
	        public NVector3 LocalAnchor2;
	
	        public sfloat BiasFactor;
	        public sfloat Softness;
	        public sfloat Distance;
	
	        public sfloat EffectiveMass;
	        public sfloat AccumulatedImpulse;
	        public sfloat Bias;
	
	        public NVector3 Jacobian;
	    }
	
	    private JHandle<SpringData> handle;
	
	    protected override void Create()
	    {
	        Trace.Assert(sizeof(SpringData) <= sizeof(ConstraintData));
	        iterate = &Iterate;
	        prepareForIteration = &PrepareForIteration;
	        handle = JHandle<ConstraintData>.AsHandle<SpringData>(Handle);
	    }
	
	    public override bool IsSmallConstraint { get; } = sizeof(SpringData) <= SmallConstraintData.ConstraintSize;
	
	    /// <summary>
	    /// Initializes the constraint.
	    /// </summary>
	    /// <param name="anchor1">Anchor point on the first rigid body, in world space.</param>
	    /// <param name="anchor2">Anchor point on the second rigid body, in world space.</param>
	    public void Initialize(NVector3 anchor1, NVector3 anchor2)
	    {
	        ref SpringData data = ref handle.Data;
	        ref RigidBodyData body1 = ref data.Body1.Data;
	        ref RigidBodyData body2 = ref data.Body2.Data;
	
	        NVector3.Subtract(anchor1, body1.Position, out data.LocalAnchor1);
	        NVector3.Subtract(anchor2, body2.Position, out data.LocalAnchor2);
	
	        data.Softness = (sfloat)0.001f;
	        data.BiasFactor = (sfloat)0.2f;
	        data.Distance = (anchor2 - anchor1).Length();
	    }
	
	    public sfloat Impulse
	    {
	        get
	        {
	            ref SpringData data = ref handle.Data;
	            return data.AccumulatedImpulse;
	        }
	    }
	
	    public NVector3 Anchor1
	    {
	        set
	        {
	            ref SpringData data = ref handle.Data;
	            ref RigidBodyData body1 = ref data.Body1.Data;
	            NVector3.Subtract(value, body1.Position, out data.LocalAnchor1);
	        }
	        get
	        {
	            ref SpringData data = ref handle.Data;
	            ref RigidBodyData body1 = ref data.Body1.Data;
	            NVector3.Add(data.LocalAnchor1, body1.Position, out NVector3 result);
	            return result;
	        }
	    }
	
	    public NVector3 Anchor2
	    {
	        set
	        {
	            ref SpringData data = ref handle.Data;
	            ref RigidBodyData body2 = ref data.Body2.Data;
	            NVector3.Subtract(value, body2.Position, out data.LocalAnchor2);
	        }
	        get
	        {
	            ref SpringData data = ref handle.Data;
	            ref RigidBodyData body2 = ref data.Body2.Data;
	            NVector3.Add(data.LocalAnchor2, body2.Position, out NVector3 result);
	            return result;
	        }
	    }
	
	    public sfloat TargetDistance
	    {
	        set
	        {
	            ref SpringData data = ref handle.Data;
	            data.Distance = value;
	        }
	        get => handle.Data.Distance;
	    }
	
	    public sfloat Distance
	    {
	        get
	        {
	            ref SpringData data = ref handle.Data;
	            ref RigidBodyData body1 = ref data.Body1.Data;
	            ref RigidBodyData body2 = ref data.Body2.Data;
	
	            NVector3.Transform(data.LocalAnchor1, body1.Orientation, out NVector3 r1);
	            NVector3.Transform(data.LocalAnchor2, body2.Orientation, out NVector3 r2);
	
	            NVector3.Add(body1.Position, r1, out NVector3 p1);
	            NVector3.Add(body2.Position, r2, out NVector3 p2);
	
	            NVector3.Subtract(p2, p1, out NVector3 dp);
	
	            return dp.Length();
	        }
	    }
	
	    public static void PrepareForIteration(ref ConstraintData constraint, sfloat idt)
	    {
	        ref SpringData data = ref Unsafe.AsRef<SpringData>(Unsafe.AsPointer(ref constraint));
	        ref RigidBodyData body1 = ref data.Body1.Data;
	        ref RigidBodyData body2 = ref data.Body2.Data;
	
	        NVector3 r1 = data.LocalAnchor1;
	        NVector3 r2 = data.LocalAnchor2;
	
	        NVector3.Add(body1.Position, r1, out NVector3 p1);
	        NVector3.Add(body2.Position, r2, out NVector3 p2);
	
	        NVector3.Subtract(p2, p1, out NVector3 dp);
	
	        sfloat error = dp.Length() - data.Distance;
	
	        NVector3 n = p2 - p1;
	        if (n.LengthSquared() != (sfloat)0.0f) n.Normalize();
	
	        data.Jacobian = n;
	        data.EffectiveMass = body1.InverseMass + body2.InverseMass;
	        data.EffectiveMass += data.Softness * idt;
	        data.EffectiveMass = (sfloat)1.0f / data.EffectiveMass;
	
	        data.Bias = error * data.BiasFactor * idt;
	
	        body1.Velocity -= body1.InverseMass * data.AccumulatedImpulse * data.Jacobian;
	        body2.Velocity += body2.InverseMass * data.AccumulatedImpulse * data.Jacobian;
	    }
	
	    public sfloat Softness
	    {
	        get => handle.Data.Softness;
	        set => handle.Data.Softness = value;
	    }
	
	    public sfloat Bias
	    {
	        get => handle.Data.BiasFactor;
	        set => handle.Data.BiasFactor = value;
	    }
	
	    public static void Iterate(ref ConstraintData constraint, sfloat idt)
	    {
	        ref SpringData data = ref Unsafe.AsRef<SpringData>(Unsafe.AsPointer(ref constraint));
	        ref RigidBodyData body1 = ref constraint.Body1.Data;
	        ref RigidBodyData body2 = ref constraint.Body2.Data;
	
	        sfloat jv = (body2.Velocity - body1.Velocity) * data.Jacobian;
	
	        sfloat softnessScalar = data.AccumulatedImpulse * data.Softness * idt;
	
	        sfloat lambda = -data.EffectiveMass * (jv + data.Bias + softnessScalar);
	
	        sfloat oldacc = data.AccumulatedImpulse;
	        data.AccumulatedImpulse += lambda;
	        lambda = data.AccumulatedImpulse - oldacc;
	
	        body1.Velocity -= body1.InverseMass * lambda * data.Jacobian;
	        body2.Velocity += body2.InverseMass * lambda * data.Jacobian;
	    }
	}
}
